package com.android.openapp.ui;

import com.android.openapp.R;
import com.android.openapp.services.Controller;
import com.android.openapp.services.Controller.ControllerBinder;

import android.content.Context;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;

public class SettingsFragment extends Fragment implements FragmentInteract{

	private CheckBox checkBox1;
	private CheckBox checkBox2;
	private CheckBox checkBox3;
	private CheckBox checkBox4;
	private CheckBox checkBox5;
	private CheckBox checkBox6;
	
	private Spinner spinner;
	
	private Button mSaveButton;
	
	public final static String RANGE = "Range";
	/*
	private final static String MUSEI = "MUSEI";
	private final static String OSTELLI = "OSTELLI";
	private final static String ARCHITETTURE = "ARCHITETTURE";
	private final static String AGRITURISMI = "AGRITURISMI";
	private final static String FIERE = "FIERE";
	*/
	
	private MainActivity mActivity;
	
	/**
	 * empty constructor, as requested from Android documentation
	 */
	public SettingsFragment(){
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.settings, container, false);
		
		checkBox1 = (CheckBox) v.findViewById(R.id.checkBox1);
		checkBox2 = (CheckBox) v.findViewById(R.id.checkBox2);
		checkBox3 = (CheckBox) v.findViewById(R.id.checkBox3);
		checkBox4 = (CheckBox) v.findViewById(R.id.checkBox4);
		checkBox5 = (CheckBox) v.findViewById(R.id.checkBox5);
		checkBox6 = (CheckBox) v.findViewById(R.id.checkBox6);
		
		spinner = (Spinner) v.findViewById(R.id.spinner);
		
		String[] names = getResources().getStringArray(R.array.places);
		checkBox1.setText(names[0]);
		checkBox2.setText(names[1]);
		checkBox3.setText(names[2]);
		checkBox4.setText(names[3]);
		checkBox5.setText(names[4]);
		checkBox6.setText(names[5]);
		
		mSaveButton = (Button) v.findViewById(R.id.settingsButton);
		mSaveButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//SAVE SETTINGS
				SharedPreferences.Editor prefsEditor = mActivity.getSharedPreferences(Controller.MY_PREFS,
						Context.MODE_PRIVATE).edit();
				prefsEditor.putBoolean(checkBox1.getText().toString(), checkBox1.isChecked());
				prefsEditor.putBoolean(checkBox2.getText().toString(), checkBox2.isChecked());
				prefsEditor.putBoolean(checkBox3.getText().toString(), checkBox3.isChecked());
				prefsEditor.putBoolean(checkBox4.getText().toString(), checkBox4.isChecked());
				prefsEditor.putBoolean(checkBox5.getText().toString(), checkBox5.isChecked());
				prefsEditor.putBoolean(checkBox6.getText().toString(), checkBox6.isChecked());
				
				prefsEditor.putInt(RANGE, spinner.getSelectedItemPosition());
				
				prefsEditor.commit();
				
				//mostra tab lista
				Intent i = new Intent(MainActivity.VIEW_PLACES_TAB);
				getActivity().sendBroadcast(i);
				Utils.showToast(getActivity(), R.string.settings_saved);
			}
		});
		
		return v;
	}
	
	ControllerBinder mBinder;
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		mActivity = (MainActivity) getActivity();
		
		mActivity.setCurrentFragment(this);
		//prendo il binder ogni volta dalla MainActivity
		mBinder = mActivity.mBinder;
		/*if (mBinder != null){
			loadSettings();
		}*/
		Log.v(MainActivity.TAG, "SettingsFragment.onActivityCreated()");
	}
	
	@Override
	public void onStart() {
		super.onStart();
		// quando entriamo nella schermata recuperiamo le impostazioni
		if (mActivity != null && mBinder != null){
			loadSettings();
		}
	
	}
	
	@Override
	public void onStop() {
		super.onStop();
	}
	
	public void loadSettings(){
		Log.v(MainActivity.TAG, "loading settings");
		SharedPreferences prefs = mActivity.getSharedPreferences(Controller.MY_PREFS,
				Context.MODE_PRIVATE);
		checkBox1.setChecked(prefs.getBoolean(checkBox1.getText().toString(), false));
		checkBox2.setChecked(prefs.getBoolean(checkBox2.getText().toString(), false));
		checkBox3.setChecked(prefs.getBoolean(checkBox3.getText().toString(), false));
		checkBox4.setChecked(prefs.getBoolean(checkBox4.getText().toString(), false));
		checkBox5.setChecked(prefs.getBoolean(checkBox5.getText().toString(), false));
		checkBox6.setChecked(prefs.getBoolean(checkBox6.getText().toString(), false));
		
		spinner.setSelection(prefs.getInt(RANGE, 0));
	}

	/**
	 * called when the onServiceConnected() method in the Activity is called
	 */
	@Override
	public void onBind(ControllerBinder binder) {
		mBinder = binder;
		loadSettings();
	}

	@Override
	public void onDataReceived() {
		//do nothing	
	}

	@Override
	public void onLocationReceived(int listIndex) {
		//do nothing
	}

	@Override
	public void onBookmarksListUpdated() {
		//do nothing
	}

}
